package cn.edu.sspu.www.exception;

import java.util.HashMap;
import java.util.Map;

/**
 * ErrorCode 异常错误码<br>
 * @author warm
 * @version 2019年07月31日
 */
public enum ErrorCode implements BaseErrorType {

    // 公共错误类型
    PAGE_NOT_FOUND(ErrorCode.CODE_PAGE_NOT_FOUND),
    SERVER_EXCEPTION(ErrorCode.CODE_SERVER_EXCEPTION),
    SERVER_NOT_FOUND(ErrorCode.CODE_SERVER_NOT_FOUND),
    SERVER_TIMEOUT(ErrorCode.CODE_SERVER_TIMEOUT),

    PARAM_NULL(ErrorCode.CODE_PARAM_NULL),
    PARAM_CHECK_FAIL(ErrorCode.CODE_PARAM_CHECK_FAIL),
    PARAM_INVALID(ErrorCode.CODE_PARAM_INVALID),

    DATA_NULL(ErrorCode.CODE_DATA_NULL),
    DATA_NOT_EXIST(ErrorCode.CODE_DATA_NOT_EXIST),
    DATA_DUPLICATION(ErrorCode.CODE_DATA_DUPLICATION),
    DATA_ERROR(ErrorCode.CODE_DATA_ERROR),

    UPLOAD_FILE_FORMAT_ERROR(ErrorCode.CODE_UPLOAD_FILE_FORMAT_ERROR),
    UPLOAD_FILE_OVER_RANGE(ErrorCode.CODE_UPLOAD_FILE_OVER_RANGE),
    FILE_UPLOAD_ERROR(ErrorCode.CODE_FILE_UPLOAD_ERROR),
    USER_NO_ACCESS(ErrorCode.CODE_USER_NO_ACCESS),
    
    ORDER_INVALID(ErrorCode.CODE_ORDER_INVALID),    
    ORDER_NOT_SUPPORT_RERUND(ErrorCode.CODE_ORDER_NOT_SUPPORT_RERUND), 
    ORDER_REFUND_FAIL(ErrorCode.CODE_ORDER_REFUND_FAIL),
    
	TRANSCEIVER_AUTH_FAIL(ErrorCode.CODE_TRANSCEIVER_AUTH_FAIL)
    
	;
	
	  /** 交易信息管理(1000~1099) */
    protected static final int CODE_ORDER_INVALID = 1001; 
    protected static final int CODE_ORDER_NOT_SUPPORT_RERUND = 1002;
    protected static final int CODE_ORDER_REFUND_FAIL = 1003;
    

	/** transceiver(1100~1199) */
    protected static final int CODE_TRANSCEIVER_AUTH_FAIL = 1101;
    
    private static final Map<Integer, String> messageMap = new HashMap<Integer, String>();

    static {
        // 公共错误码默认提示信息，注意：
        // 1> 公共提示默认通过map来赋初始值，也可以在errormsg.propertes文件中重写;
        // 2> 业务模块提示信息不需要在map中添加，只需要在errormsg.propertes文件添加即可
        if (messageMap.size() == 0) {
            messageMap.put(CODE_PAGE_NOT_FOUND, "页面地址找不到");
            messageMap.put(CODE_SERVER_EXCEPTION, "服务端错误");
            messageMap.put(CODE_SERVER_NOT_FOUND, "服务无法访问");
            messageMap.put(CODE_SERVER_TIMEOUT, "服务访问超时");
            messageMap.put(CODE_PARAM_NULL, "参数为空");
            messageMap.put(CODE_PARAM_CHECK_FAIL, "参数校验失败");
            messageMap.put(CODE_PARAM_INVALID, "参数格式不正确");
            messageMap.put(CODE_DATA_NULL, "数据为空");
            messageMap.put(CODE_DATA_NOT_EXIST, "数据不存在");
            messageMap.put(CODE_DATA_DUPLICATION, "数据重复");
            messageMap.put(CODE_DATA_ERROR, "数据错误");
            messageMap.put(CODE_UPLOAD_FILE_FORMAT_ERROR, "上传文件格式有误");
            messageMap.put(CODE_UPLOAD_FILE_OVER_RANGE, "上传文件大小超过给定范围");
            messageMap.put(CODE_FILE_UPLOAD_ERROR, "上传文件出错");
            messageMap.put(CODE_USER_NO_ACCESS, "没有权限");
        }

    }

    private int errorCode;

    ErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    /**
     * 获取错误码值
     * 
     * @return
     */
    public int getCode() {
        return this.errorCode;
    }

    /**
     * 获取错误码对应的错误信息
     * 
     * @return
     */
    public String getMessage() {
        String message = messageMap.get(this.errorCode);
        if (message == null) {
            message = "未知异常";
        }
        return message;
    }

    @Override
    public String toString() {
        return getMessage();
    };

    public static ErrorCode fromValue(int errorCode) {
        switch (errorCode) {
            case CODE_PAGE_NOT_FOUND:
                return PAGE_NOT_FOUND;
            case CODE_SERVER_NOT_FOUND:
                return SERVER_NOT_FOUND;
            case CODE_SERVER_TIMEOUT:
                return SERVER_TIMEOUT;
            case CODE_PARAM_NULL:
                return PARAM_NULL;
            case CODE_PARAM_CHECK_FAIL:
                return PARAM_CHECK_FAIL;
            case CODE_PARAM_INVALID:
                return PARAM_INVALID;
            case CODE_DATA_NULL:
                return DATA_NULL;
            case CODE_DATA_NOT_EXIST:
                return DATA_NOT_EXIST;
            case CODE_DATA_DUPLICATION:
                return DATA_DUPLICATION;
            default:
                throw new IllegalArgumentException("invalid error code value " + errorCode);
        }
    }

    public static boolean isValid(byte value) {
        try {
            fromValue(value);
            return true;
        } catch (IllegalArgumentException e) {
            return false;
        }
    }

}
